<?php
class Admin_ApparatusController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pDepartment = empty($p['department'])?'':$p['department'];
        $statusid = empty($p['statusid'])?'':$p['statusid'];
        $tAMO = new ApplyrepModel;
        $tMO = new ApparatusModel;
        $tAdMO = new AdminModel;
        $tWMO = new WorkerModel;
        $tDIMO = new DepartmentinfoModel;
        $department = $tDIMO->field('num,introduce')->fList();
        $tWhere = ' 1 ';
        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and department = '.$tAData[0]['department'];
            }
        }

        if(!empty($pDepartment)){
            $tWhere .= ' and department = '.$pDepartment;
        }
        if(!empty($statusid)){
            $tWhere .= ' and status = '.$statusid;
        }
        
        $tTime = time();

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('ID出错'):Tool_Fnc::safe_string($p['id']);
            $pRid = empty($p['rid'])?Tool_Fnc::ajaxMsg('rid出错'):Tool_Fnc::safe_string($p['rid']);
            $pDepartmentnum = empty($p['department_num'])?Tool_Fnc::ajaxMsg('部门ID出错'):Tool_Fnc::safe_string($p['department_num']);

            $tData = array(
                'id' => $pId,
                'status' => 2,
                'department' => $pDepartmentnum,
                'updated' => $tTime, 
            );
            $tMO->begin();
            if(!$tMO->update($tData)){
                Tool_Fnc::ajaxMsg('操作失败');
            }
            //更新采购清单状态
            $tData = array(
                'rep_id' => $pRid,
                'rep_status' => 2,
            );
            if(!$tAMO->update($tData)){
                $tMO->back();
                Tool_Fnc::ajaxMsg('操作失败 002');
            }
            $tMO->commit();
            Tool_Fnc::ajaxMsg('操作成功',1);
        }
        $tCount = $tMO->field('count(id) c')->where($tWhere)->fList();
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        $tDatas = array();
        if(!empty($tCnt)){
            $tDatas = $tMO->field('id,r_id,status,department')->where($tWhere)->order('id desc')->limit($tPage->limit())->fList();
            if(!empty($tDatas)){
                foreach($tDatas as $tK=>$tV){
                    $tAData = $tAMO->field('id,rep_name,rep_type,rep_time,rep_rea,rep_sapre')->where('rep_id = '.$tV['r_id'])->fRow();
                    // print_r($tAData);
                    $tDatas[$tK]['rep_name'] = $tAData['rep_name'];
                    $tDatas[$tK]['rep_type'] = $tAData['rep_type'];
                    $tDatas[$tK]['rep_time'] = $tAData['rep_time'];
                    $tDatas[$tK]['rep_rea'] = $tAData['rep_rea'];
                    $tDatas[$tK]['rep_sapre'] = $tAData['rep_sapre'];
                    //获取部门''
                    $tSql = 'select introduce,num from '.$tDIMO->table.' where  num = '.$tV['department'];
                    $tAdData = $tAdMO->query($tSql);
                    // print_r($tSql);die;
                    $tDatas[$tK]['department'] = $tAdData[0]['introduce'];
                    // $tDatas[$tK]['department_num'] = $tAdData[0]['department_num'];
                }
            }
        }

        //部门显示
        $tDIDatas = $tDIMO->field('num,introduce')->fList();
        $this->assign('pDepartment',$pDepartment);
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tShow',$tPage->show());
        $this->assign('tDatas',$tDatas);
        $this->assign('department',$department);
        $this->assign('statusid',$statusid);
    }
}
?>
